import { RuleSetObject } from '@aws-sdk/util-endpoints'

export const validRules:RuleSetObject[]=[
  {
    "version": "1.3",
    "parameters": {
      "region": {
        "type": "string",
        "builtIn": "AWS::Region",
        "required": true
      },
      "useDualStackEndpoint": {
        "type": "boolean",
        "builtIn": "AWS::UseDualStack"
      },
      "useFIPSEndpoint": {
        "type": "boolean",
        "builtIn": "AWS::UseFIPS"
      },
      "endpointId": {
        "type": "string"
      }
    },
    "rules": [
      {
        "conditions": [
          {
            "fn": "aws.partition",
            "argv": [
              {
                "ref": "region"
              }
            ],
            "assign": "partitionResult"
          }
        ],
        "rules": [
          {
            "conditions": [
              {
                "fn": "isSet",
                "argv": [
                  {
                    "ref": "endpointId"
                  }
                ]
              }
            ],
            "rules": [
              {
                "conditions": [
                  {
                    "fn": "isSet",
                    "argv": [
                      {
                        "ref": "useFIPSEndpoint"
                      }
                    ]
                  },
                  {
                    "fn": "booleanEquals",
                    "argv": [
                      {
                        "ref": "useFIPSEndpoint"
                      },
                      true
                    ]
                  }
                ],
                "error": "FIPS endpoints not supported with multi-region endpoints",
                "type": "error"
              },
              {
                "conditions": [
                  {
                    "fn": "not",
                    "argv": [
                      {
                        "fn": "isSet",
                        "argv": [
                          {
                            "ref": "useFIPSEndpoint"
                          }
                        ]
                      }
                    ]
                  },
                  {
                    "fn": "isSet",
                    "argv": [
                      {
                        "ref": "useDualStackEndpoint"
                      }
                    ]
                  },
                  {
                    "fn": "booleanEquals",
                    "argv": [
                      {
                        "ref": "useDualStackEndpoint"
                      },
                      true
                    ]
                  }
                ],
                "endpoint": {
                  "url": "https://{endpointId}.endpoint.events.{partitionResult#dualStackDnsSuffix}",
                  "properties": {
                    "authSchemes": [
                      {
                        "name": "sigv4a",
                        "signingName": "events",
                        "signingRegionSet": ["*"]
                      }
                    ]
                  }
                },
                "type": "endpoint"
              },
              {
                "conditions": [],
                "endpoint": {
                  "url": "https://{endpointId}.endpoint.events.{partitionResult#dnsSuffix}",
                  "properties": {
                    "authSchemes": [
                      {
                        "name": "sigv4a",
                        "signingName": "events",
                        "signingRegionSet": ["*"]
                      }
                    ]
                  }
                },
                "type": "endpoint"
              }
            ],
            "type": "tree"
          },
          {
            "conditions": [
              {
                "fn": "isValidHostLabel",
                "argv": [
                  {
                    "ref": "region"
                  },
                  false
                ]
              }
            ],
            "rules": [
              {
                "conditions": [
                  {
                    "fn": "isSet",
                    "argv": [
                      {
                        "ref": "useFIPSEndpoint"
                      }
                    ]
                  },
                  {
                    "fn": "booleanEquals",
                    "argv": [
                      {
                        "ref": "useFIPSEndpoint"
                      },
                      true
                    ]
                  },
                  {
                    "fn": "not",
                    "argv": [
                      {
                        "fn": "isSet",
                        "argv": [
                          {
                            "ref": "useDualStackEndpoint"
                          }
                        ]
                      }
                    ]
                  }
                ],
                "endpoint": {
                  "url": "https://events-fips.{region}.{partitionResult#dnsSuffix}",
                  "properties": {
                    "authSchemes": [
                      {
                        "name": "sigv4a",
                        "signingName": "events",
                        "signingRegionSet": ["*"]
                      }
                    ]
                  }
                },
                "type": "endpoint"
              },
              {
                "conditions": [
                  {
                    "fn": "isSet",
                    "argv": [
                      {
                        "ref": "useDualStackEndpoint"
                      }
                    ]
                  },
                  {
                    "fn": "booleanEquals",
                    "argv": [
                      {
                        "ref": "useDualStackEndpoint"
                      },
                      true
                    ]
                  },
                  {
                    "fn": "not",
                    "argv": [
                      {
                        "fn": "isSet",
                        "argv": [
                          {
                            "ref": "useFIPSEndpoint"
                          }
                        ]
                      }
                    ]
                  }
                ],
                "endpoint": {
                  "url": "https://events.{region}.{partitionResult#dualStackDnsSuffix}",
                  "properties": {
                    "authSchemes": [
                      {
                        "name": "sigv4a",
                        "signingName": "events",
                        "signingRegionSet": ["*"]
                      }
                    ]
                  }
                },
                "type": "endpoint"
              },
              {
                "conditions": [
                  {
                    "fn": "isSet",
                    "argv": [
                      {
                        "ref": "useDualStackEndpoint"
                      }
                    ]
                  },
                  {
                    "fn": "isSet",
                    "argv": [
                      {
                        "ref": "useFIPSEndpoint"
                      }
                    ]
                  },
                  {
                    "fn": "booleanEquals",
                    "argv": [
                      {
                        "ref": "useDualStackEndpoint"
                      },
                      true
                    ]
                  },
                  {
                    "fn": "booleanEquals",
                    "argv": [
                      {
                        "ref": "useFIPSEndpoint"
                      },
                      true
                    ]
                  }
                ],
                "endpoint": {
                  "url": "https://events-fips.{region}.{partitionResult#dualStackDnsSuffix}",
                  "properties": {
                    "authSchemes": [
                      {
                        "name": "sigv4a",
                        "signingName": "events",
                        "signingRegionSet": ["*"]
                      }
                    ]
                  }
                },
                "type": "endpoint"
              },
              {
                "conditions": [],
                "endpoint": {
                  "url": "https://events.{region}.{partitionResult#dnsSuffix}"
                },
                "type": "endpoint"
              }
            ],
            "type": "tree"
          },
          {
            "conditions": [],
            "error": "{region} is not a valid HTTP host-label",
            "type": "error"
          }
        ],
        "type": "tree"
      }
    ]
  },
  {
    "parameters": {
      "Uri": {
        "type": "string",
        "documentation": "A URI to use"
      },
      "Arn": {
        "type": "string",
        "documentation": "an ARN to extract fields from"
      },
      "CustomError": {
        "type": "string",
        "documentation": "when set, a custom error message"
      }
    },
    "rules": [
      {
        "documentation": "when URI is set, use it directly",
        "conditions": [
          {
            "fn": "isSet",
            "argv": [
              {
                "ref": "Uri"
              }
            ]
          },
          {
            "fn": "isSet",
            "argv": [
              {
                "ref": "Arn"
              }
            ]
          },
          {
            "fn": "aws.parseArn",
            "argv": [
              {
                "ref": "Arn"
              }
            ],
            "assign": "parsedArn"
          }
        ],
        "endpoint": {
          "url": {
            "ref": "Uri"
          },
          "headers": {
            "x-uri": [
              {
                "ref": "Uri"
              }
            ],
            "x-arn-region": [
              {
                "fn": "getAttr",
                "argv": [
                  {
                    "ref": "parsedArn"
                  },
                  "region"
                ]
              }
            ]
          }
        },
        "type": "endpoint"
      },
      {
        "documentation": "A custom error",
        "conditions": [
          {
            "fn": "isSet",
            "argv": [
              {
                "ref": "CustomError"
              }
            ]
          }
        ],
        "type": "error",
        "error": {
          "ref": "CustomError"
        }
      },
      {
        "type": "error",
        "conditions": [
          {
            "fn": "isSet",
            "argv": [
              {
                "ref": "Arn"
              }
            ]
          },
          {
            "fn": "aws.parseArn",
            "argv": [
              {
                "ref": "Arn"
              }
            ],
            "assign": "parsedArn"
          }
        ],
        "error": {
          "fn": "getAttr",
          "argv": [
            {
              "ref": "parsedArn"
            },
            "partition"
          ]
        }
      },
      {
        "documentation": "fallback when nothing is set",
        "conditions": [],
        "error": "No fields were set",
        "type": "error"
      }
    ],
    "version": "1.3"
  },
  {
    "version": "1.3",
    "parameters": {
      "BucketName": {
        "type": "string",
        "required": true,
        "documentation": "the input used to test isVirtualHostableS3Bucket"
      }
    },
    "rules": [
      {
        "conditions": [
          {
            "fn": "aws.isVirtualHostableS3Bucket",
            "argv": ["{BucketName}", false]
          }
        ],
        "endpoint": {
          "url": "https://{BucketName}.s3.amazonaws.com"
        },
        "type": "endpoint"
      },
      {
        "conditions": [
          {
            "fn": "aws.isVirtualHostableS3Bucket",
            "argv": ["{BucketName}", true]
          }
        ],
        "endpoint": {
          "url": "http://{BucketName}.s3.amazonaws.com"
        },
        "type": "endpoint"
      },
      {
        "conditions": [],
        "error": "not isVirtualHostableS3Bucket",
        "type": "error"
      }
    ]
  },
  {
    "version": "1.3",
    "parameters": {
      "Region": {
        "type": "string",
        "builtIn": "AWS::Region"
      },
      "Bucket": {
        "type": "string"
      },
      "TestCaseId": {
        "type": "string"
      }
    },
    "rules": [
      {
        "documentation": "tests of invalid arns",
        "conditions": [
          {
            "fn": "isSet",
            "argv": [
              {
                "ref": "TestCaseId"
              }
            ]
          },
          {
            "fn": "isSet",
            "argv": [
              {
                "ref": "Bucket"
              }
            ]
          },
          {
            "fn": "stringEquals",
            "argv": ["{TestCaseId}", "invalid-arn"]
          }
        ],
        "type": "tree",
        "rules": [
          {
            "conditions": [
              {
                "fn": "aws.parseArn",
                "argv": ["{Bucket}"]
              }
            ],
            "type": "error",
            "error": "A valid ARN was parsed but `{Bucket}` is not a valid ARN"
          },
          {
            "conditions": [],
            "type": "error",
            "error": "Test case passed: `{Bucket}` is not a valid ARN."
          }
        ]
      },
      {
        "documentation": "tests of valid arns",
        "conditions": [
          {
            "fn": "isSet",
            "argv": [
              {
                "ref": "TestCaseId"
              }
            ]
          },
          {
            "fn": "isSet",
            "argv": [
              {
                "ref": "Bucket"
              }
            ]
          },
          {
            "fn": "stringEquals",
            "argv": ["{TestCaseId}", "valid-arn"]
          }
        ],
        "type": "tree",
        "rules": [
          {
            "conditions": [
              {
                "fn": "aws.parseArn",
                "argv": ["{Bucket}"],
                "assign": "arn"
              },
              {
                "fn": "getAttr",
                "argv": [{ "ref": "arn" }, "resourceId[0]"],
                "assign": "resource"
              }
            ],
            "type": "error",
            "error": "Test case passed: A valid ARN was parsed: service: `{arn#service}`, partition: `{arn#partition}, region: `{arn#region}`, accountId: `{arn#accountId}`, resource: `{resource}`"
          },
          {
            "conditions": [],
            "type": "error",
            "error": "Test case failed: `{Bucket}` is a valid ARN but parseArn failed to parse it."
          }
        ]
      },
      {
        "documentation": "region is set",
        "conditions": [
          {
            "fn": "isSet",
            "argv": [
              {
                "ref": "Region"
              }
            ]
          },
          {
            "fn": "aws.partition",
            "argv": ["{Region}"],
            "assign": "partitionResult"
          }
        ],
        "rules": [
          {
            "documentation": "bucket is set, handle bucket specific endpoints",
            "conditions": [
              {
                "fn": "isSet",
                "argv": [
                  {
                    "ref": "Bucket"
                  }
                ]
              }
            ],
            "rules": [
              {
                "documentation": "bucket is set and is an arn",
                "conditions": [
                  {
                    "fn": "aws.parseArn",
                    "argv": [
                      {
                        "ref": "Bucket"
                      }
                    ],
                    "assign": "bucketArn"
                  }
                ],
                "rules": [
                  {
                    "conditions": [
                      {
                        "fn": "getAttr",
                        "argv": [
                          {
                            "ref": "bucketArn"
                          },
                          "resourceId[1]"
                        ],
                        "assign": "outpostId"
                      }
                    ],
                    "rules": [
                      {
                        "conditions": [
                          {
                            "fn": "stringEquals",
                            "argv": ["{outpostId}", ""]
                          }
                        ],
                        "error": "OutpostId was empty",
                        "type": "error"
                      },
                      {
                        "conditions": [],
                        "endpoint": {
                          "url": "https://{outpostId}-{bucketArn#accountId}.{bucketArn#region}.{partitionResult#dnsSuffix}"
                        },
                        "type": "endpoint"
                      }
                    ],
                    "type": "tree"
                  },
                  {
                    "conditions": [],
                    "error": "Invalid ARN: outpostId was not set",
                    "type": "error"
                  }
                ],
                "type": "tree"
              },
              {
                "documentation": "bucket can be used as a host label",
                "conditions": [
                  {
                    "fn": "isValidHostLabel",
                    "argv": ["{Bucket}", false]
                  }
                ],
                "endpoint": {
                  "url": "https://{Bucket}.{Region}.amazonaws.com"
                },
                "type": "endpoint"
              },
              {
                "conditions": [],
                "documentation": "fallback: use bucket in the path",
                "endpoint": {
                  "url": "https://{Region}.amazonaws.com/{Bucket}"
                },
                "type": "endpoint"
              }
            ],
            "type": "tree"
          },
          {
            "documentation": "region is set, bucket is not",
            "conditions": [],
            "endpoint": {
              "url": "https://{Region}.{partitionResult#dnsSuffix}"
            },
            "type": "endpoint"
          }
        ],
        "type": "tree"
      },
      {
        "documentation": "fallback when region is unset",
        "conditions": [],
        "error": "Region must be set to resolve a valid endpoint",
        "type": "error"
      }
    ]
  },
  {
    "parameters": {
      "Region": {
        "type": "string",
        "builtIn": "AWS::Region",
        "required": true
      },
      "PropertyOne": {
        "type": "boolean"
      },
      "PropertyTwo": {
        "type": "string"
      },
      "PropertyThree": {
        "type": "boolean"
      }
    },
    "rules": [
      {
        "documentation": "base rule",
        "conditions": [
          {
            "fn": "aws.partition",
            "argv": [
              {
                "ref": "Region"
              }
            ],
            "assign": "PartResult"
          }
        ],
        "rules": [
          {
            "documentation": "the AWS partition",
            "conditions": [
              {
                "fn": "stringEquals",
                "argv": [
                  "aws",
                  {
                    "fn": "getAttr",
                    "argv": [
                      {
                        "ref": "PartResult"
                      },
                      "name"
                    ]
                  }
                ]
              }
            ],
            "endpoint": {
              "url": "https://aws-partition.{Region}.{PartResult#dnsSuffix}",
              "properties": {
                "authSchemes": [
                  {
                    "name": "sigv4",
                    "signingName": "serviceName",
                    "signingRegion": "{Region}"
                  }
                ],
                "meta": {
                  "baseSuffix": "{PartResult#dnsSuffix}",
                  "dualStackSuffix": "{PartResult#dualStackDnsSuffix}"
                }
              }
            },
            "type": "endpoint"
          },
          {
            "documentation": "the other partitions",
            "conditions": [],
            "endpoint": {
              "url": "https://{PartResult#name}.{Region}.{PartResult#dnsSuffix}",
              "properties": {
                "authSchemes": [
                  {
                    "name": "sigv4",
                    "signingName": "serviceName",
                    "signingRegion": "{Region}"
                  }
                ],
                "meta": {
                  "baseSuffix": "{PartResult#dnsSuffix}",
                  "dualStackSuffix": "{PartResult#dualStackDnsSuffix}"
                }
              }
            },
            "type": "endpoint"
          },
          {
            "conditions": [],
            "error": "no rules matched",
            "type": "error"
          }
        ],
        "type": "tree"
      }
    ],
    "version": "1.3"
  }
]